package com.wjk.kylin.mall.order.mq;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;


@Slf4j
@Component
public class RabbitConsumer {

    @Autowired
    private MyConsumer myConsumer;

    /**
     *
     * @param channel
     * @param message
     */
    @RabbitListener(queues = DelayedRabbitConfig.DELAYED_QUEUE)
    public void processTtlMsg(Channel channel, Message message) {
        try {
            String msg = new String(message.getBody());
            MessageDTO bo = JSON.parseObject(msg, MessageDTO.class);
            log.info("开始消费延时消息-收到的消息是:{}", msg);

            Boolean flag = myConsumer.consumer(bo);

            log.info("结束消费延时消息-收到的消息是:{},flag:{}", msg,flag);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}